#include "changeidwidget.h"

#include<QFile>
#include<QDir>
#include<qDebug>
#include<QMessageBox>
#include<QLineEdit>
#include<QHBoxLayout>
#include<QSpacerItem>
#include<QLabel>
#include<QPushButton>
#include<QApplication>
#include<QProcess>
#include"windows.h"
#include"shellapi.h"
#include"Winbase.h"
#include<QDateTime>
#include<QThread>

#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"

ChangeIdWidget::ChangeIdWidget(QWidget *parent) :
  QWidget(parent,Qt::WindowStaysOnTopHint)
{

  create();

  connect(btn1,SIGNAL(clicked()),this,SLOT(btn1Slots()));

  connect(btn2,SIGNAL(clicked()),this,SLOT(btn2Slots()));

}

ChangeIdWidget::~ChangeIdWidget()
{
  delete lineEdit;
  lineEdit = nullptr;
  delete btn1;
  btn1 = nullptr;
  delete btn2;
  btn2 = nullptr;
}

void ChangeIdWidget::create()
{

  setWindowTitle("更改系统ID");
  btn1 = new QPushButton("取消",this);
  btn1->setFixedSize(180,60);
  btn2 = new QPushButton("确定",this);
  btn2->setFixedSize(180,60);

  QVBoxLayout *v_layout = new QVBoxLayout(this);
  QHBoxLayout *h_layout = new QHBoxLayout;

  lineEdit = new QLineEdit(this);
  lineEdit->setPlaceholderText("请输入数字！");
  lineEdit->setFixedSize(360,60);
  setStyleSheet("QPushButton,QLineEdit{width: 100px;height: 100px;font-size: 30px;font-bold;font-family: 微软雅黑;}");
  QSpacerItem *v_spacer = new QSpacerItem(10,10,QSizePolicy::Minimum,QSizePolicy::Expanding);
  QSpacerItem *h_spacer = new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePolicy::Minimum);

  h_layout->addWidget(btn1);
  h_layout->addItem(h_spacer);
  h_layout->addWidget(btn2);

  v_layout->addWidget(lineEdit);
  v_layout->addItem(v_spacer);
  v_layout->addLayout(h_layout);

  setLayout(v_layout);

}

//void ChangeIdWidget::writeText(const QString& data)
//{
//    try{
//  QString filename = QApplication::applicationDirPath() + "/config2.txt";
//  QFileInfo fileInfo(filename);

//  if (!fileInfo.exists()) {
//      QFile file(filename);

//      if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
//          qDebug()<<"Can't open the file!";
//          return;
//        }
//      file.close();
//    }

//  QFile file(filename);

//  if(!file.open(QIODevice::Append | QIODevice::Text))
//    {
//      qDebug()<<"Can't open the file!";
//      return;
//    }

//  file.write(data.toUtf8());
//   MYLOG<<data.toUtf8();
//  file.close();
//  }
//  catch(std::exception &ex)
//  {
//    MYLOG<<ex.what();
//     qWarning()<<"ChangeIdWidget"<<ex.what();
//    return ;
//  }

//}

void ChangeIdWidget::btn1Slots()
{
  lineEdit->clear();
}

void ChangeIdWidget::btn2Slots()
{
  QString data = lineEdit->text();

  if(data == "")
    {
      QMessageBox::information(this,tr("提示"),tr("请输入数据!"),QMessageBox::Ok);
    }else{
      emit mySignalId(data);
//      writeText("\n"+data);
    }
}



